package com.qf.service.imle;

import com.qf.pojo.BaseResp;
import com.qf.service.MvSearchService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MvSearchImpl  implements MvSearchService {
    @Autowired
    RestHighLevelClient restHighLevelClient;
    @Override
    public BaseResp findAllBykey(String key) {
        BaseResp baseResp = new BaseResp();
        SearchRequest searchRequest = new SearchRequest("bcy-search");
        searchRequest.types("doc");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        SearchSourceBuilder query=null;
        if(!StringUtils.isEmpty(key)){
            MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(key, "mvName", "mvDesc");
            //设置分页得参数
//            searchSourceBuilder.from(0);
//           searchSourceBuilder.size(10);
            query = searchSourceBuilder.query(multiMatchQueryBuilder);
        }else {
            MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
            //设置分页得参数
//            searchSourceBuilder.from(0);
//            searchSourceBuilder.size(10);
            query = searchSourceBuilder.query(matchAllQueryBuilder);
        }
        //设置高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("mvName");
        highlightBuilder.field("mvDesc");
        highlightBuilder.preTags("<font style='color:red'>");
        highlightBuilder.postTags("</font>");
        searchSourceBuilder.highlighter(highlightBuilder);
        searchRequest.source(query);
        try {
            SearchResponse search = restHighLevelClient.search(searchRequest);
            SearchHits hits = search.getHits();
            baseResp.setTotal(hits.getTotalHits());
            SearchHit[] hits1 = hits.getHits();
            List list = new ArrayList<>();
            for (SearchHit hi:hits1
            ) {
                Map<String, HighlightField> highlightFields = hi.getHighlightFields();
                HighlightField shopName = highlightFields.get("mvName");
                HighlightField shopDesc = highlightFields.get("mvDesc");
                if (shopName != null) {
                    Text[] fragments = shopName.getFragments();
                    if (fragments != null) {
                        StringBuffer stringBuffer = new StringBuffer();
                        for (Text fre : fragments
                        ) {
                            stringBuffer.append(fre);
                        }
                        Map<String, Object> sourceAsMap = hi.getSourceAsMap();
                        sourceAsMap.put("mvName", stringBuffer.toString());
                    }
                }
                if(shopDesc!=null){
                    Text[] fragments = shopDesc.getFragments();
                    if(fragments!=null){
                        StringBuffer stringBuffer = new StringBuffer();
                        for (Text fre: fragments
                        ) {
                            stringBuffer.append(fre);
                        }
                        Map<String, Object> sourceAsMap = hi.getSourceAsMap();
                        sourceAsMap.put("mvDesc",stringBuffer.toString());
                    }

                }
                list.add(hi.getSourceAsMap());
            }
            baseResp.setCode(200);
            baseResp.setData(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baseResp;

    }
}
